﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Unity;
using RialternativeSample.Client.Helpers;
using RialternativeSample.Services.Interfaces;
using RialternativeSample.Views.Interfaces;

namespace RialternativeSample.Services
{
    public class DialogService : IDialogService
    {
        private List<IView> dialogs;

        public DialogService()
        {
            this.dialogs = new List<IView>();
        }

        public void ShowDialog(Type dialogType)
        {
            IView view = UnityHelper.UnityContainer.Resolve(dialogType) as IView;
            if (view == null)
            {
                throw new InvalidOperationException();
            }

            this.dialogs.Add(view);
            view.ShowWindow();
        }

        public void CloseDialog(object dialogContext)
        {
            IView view = this.dialogs.FirstOrDefault(d => d.Context == dialogContext);
            if (view == null)
            {
                throw new InvalidOperationException();
            }

            view.CloseWindow();
        }
    }
}
